﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pulse
{
    /* Particle class taken from RB Whitaker Particle tutorial*/
    public class Particle
    {
        public Texture2D Texture { get; set; }      // The texture that will be drawn to represent the particle
        public Vector2 Position { get; set; }       // The current position of the particle        
        public Vector2 Velocity { get; set; }       // The speed of the particle at the current instance
        public Color Color { get; set; }            // The color of the particle
        public float Angle { get; set; }            // The current angle of rotation of the particle
        public float AngularVelocity { get; set; }  // The speed that the angle is changing
        public float Opacity { get; set; }          // How transparent the particle is, 1 = visible, 0 = invisible
        public float Size { get; set; }             // The size of the particle
        public float DecayRate { get; set; }        // The rate at which to make the opacity inc/decrement
        public float DrawOrder { get; set; }        // The layer at which to draw the particle at
        public int TSS { get; set; }              // The "time since start" of the particle, how long it has lived
        public int TTL { get; set; }                // The 'time to live' of the particle
        public bool isAlive
        {
            get { return TSS < TTL ? true : false; }
        }

        /* 
         * Empty constructor to be used 
         * in conjuction with the InitializeParticle method
         */
        public Particle() { }

        public void InitializeParticle(Texture2D texture, Vector2 position, Vector2 velocity,
            float angle, float angularVelocity, Color color, float opacityRate, float size, int ttl)
        {
            this.Texture = texture;
            this.Position = position;
            this.Velocity = velocity;
            this.Angle = angle;
            this.AngularVelocity = angularVelocity;
            this.Color = color;
            this.Opacity = 1.0f;
            this.Size = size;
            this.TTL = ttl;
            this.TSS = 0;
            this.DecayRate = opacityRate;
            this.DrawOrder = 0.0f;
        }

        public Particle(Texture2D texture, Vector2 position, Vector2 velocity,
            float angle, float angularVelocity, Color color, float opacityRate, float size, int ttl)
        {
            this.Texture = texture;
            this.Position = position;
            this.Velocity = velocity;
            this.Angle = angle;
            this.AngularVelocity = angularVelocity;
            this.Color = color;
            this.Opacity = 1.0f;
            this.Size = size;
            this.TSS = 0;
            this.TTL = ttl;
            this.DecayRate = opacityRate;
            this.DrawOrder = 0.0f;
        }

        public void Update()
        {
            TSS++;
            Position += Velocity;
            Angle += AngularVelocity;
            Opacity -= DecayRate;
            Velocity += new Vector2(0, -0.2f);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
            Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);

            spriteBatch.Draw(Texture, Position, sourceRectangle, Color * Opacity,
                Angle, origin, Size, SpriteEffects.None, DrawOrder);
        }

    }
}
